#pragma once

#include "DataTypes.h"
#include <vector>
#include <string>

class CacheManager {
public:
    explicit CacheManager(long long capacity = 0);

    // 对应 cm_allocate
    long long allocate(long long size_needed, int buf_id, const std::string& strategy,
        const std::unordered_map<int, int>& buf_id_to_free_step,
        int current_buf_free_step);

    // 对应 cm_allocate_at
    bool allocateAt(long long size_needed, int buf_id, long long requested_offset);

    // 对应 cm_free
    void free(int buf_id_to_free);

    long long getCapacity() const { return capacity_; }
    const std::vector<MemBlock>& getAllocatedBlocks() const { return allocated_blocks_; }
    const std::vector<MemBlock>& getFreeBlocks() const { return free_blocks_; }

    // *** NEW: Added logging function declaration ***
    void printState(const std::string& context, int step, int node_id, int buf_id, const std::string& timing) const;

private:
    long long capacity_;
    std::vector<MemBlock> allocated_blocks_;
    std::vector<MemBlock> free_blocks_;

    void mergeFreeBlocks();
};